package logic

import (
	"context"
	"rikao8/houserpc/models/mysql"

	"rikao8/houserpc/houserpc"
	"rikao8/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLogic {
	return &AddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *AddLogic) Add(in *houserpc.AddRequest) (*houserpc.AddResponse, error) {
	// todo: add your logic here and delete this line

	h := mysql.House{
		Name:    in.Name,
		Address: in.Address,
		Area:    int(in.Area),
		Price:   float64(in.Price),
	}
	err := h.Create()
	if err != nil {
		logx.Error("发布失败")
		return nil, err
	}
	return &houserpc.AddResponse{Id: int64(h.ID)}, nil
}
